var xhr  = require ('../util/xhr');
var connector = require('../util/dbconnector');
var cheerio = require('cheerio');


module.exports={
	//获取公交数据列表
	get_bus_catalog:function(callback){
		xhr.get('http://www.szjt.gov.cn/Page/catalog.xml',function(result){
			callback(result);
		})
	},
	//列表写入
	write_to_local_store:function(callback){

	},
	get_bus_stationRTInfo_by_stationCode:function(stationCode,callback){
		xhr.get('http://www.szjt.gov.cn/apts/default.aspx?StandCode='+stationCode,function(result){


			var $ = cheerio.load(result);

			var RTInfoList=$("#MainContent_DATA>table")[0].children;


			var stationRTInfoList=[];


				 for(var i in RTInfoList){
					
				
					var _lineCode_unhandled=RTInfoList[i].children[0].children[0].data || RTInfoList[i].children[0].children[0].children[0].data ;
					var _direction_unhandled=RTInfoList[i].children[1].children[0].data;
					var _nearestBusDistance_unhandled=RTInfoList[i].children[4].children[0].data;


					var _lineCode=_lineCode_unhandled;
					var _direction={};
					var _nearestBusDistance=_nearestBusDistance_unhandled;


					if(_direction_unhandled.indexOf('=>')!=-1){
						_direction.from=_direction_unhandled.split('=>')[0];
						_direction.to=_direction_unhandled.split('=>')[1];
					}
					else{
						_direction.to=_direction_unhandled.split('=>')[0];
					}

					var _stationRTInfo={
						lineCode:_lineCode,
						direction:_direction,
						nearestBusDistance:_nearestBusDistance
					}

					stationRTInfoList.push(_stationRTInfo);

				  }


				  var result={
				stationName:'',
				stationCode:stationCode,
				stationRTInfoList:stationRTInfoList
			}

				  console.log(result)

			callback(result)


			


		})
	},
	get_bus_stationCodeList_with_stationName:function(stationName,callback){
		xhr.post({
			'ctl00$MainContent$StandName':stationName,
			'ctl00$MainContent$SearchCode':'搜索'
		},function(result){
			
			var cb = {
				standName : '',
				standCode : '',
				region : '',
				road : '',
				direction : ''
			}
			callback(cb);
		})
	},
	get_bus_busLineRTInfo_by_lineGUID:function(lineGUID,callback){
		xhr.get('http://www.szjt.gov.cn/apts/APTSLine.aspx?LineGUID='+lineGUID,function(result){
			callback(result);
		})
	},
	get_bus_busDistanceToStation:function(lineGUID,stationCode,callback){
		xhr.get('http://www.szjt.gov.cn/apts/APTSLine.aspx?LineGUID='+lineGUID,function(result){
			// callback(result);


			busDistanceToStationList={
				lineCode:'',
				lineGUID:lineGUID,
				stationCode:stationCode,
				list:{
					stationCode:'',
					stationName:'',
					busCount:'',
					busInfo:'',
					distance:'',
					enterStationTime:''
				}

			}

			callback(busDistanceToStationList);
		})
	},
	get_bus_lineCode_by_lineGUID:function(lineGUID,callback){
		connector.getConnection(function(client){
			client.query('select * from `lpline` where `LPGUID`= ?',[lineGUID],function(err,result){
				if(err){
					console.log(err)
					return;
				}

				callback(result[0]);
			})

			client.release();
		})
	},
	get_bus_lineGUID_by_lineCode:function(lineCode,callback){
		connector.getConnection(function(client){
			client.query('select * from `lpline` where `LPLineName` like ? ',['%'+lineCode.trim()+'%'],function(err,result){
				if(err){
					console.log(err)
					return;
				}

				callback(result[0]);
			})

			client.release();
		})
	}

}